package com.szy.news.activity;

import java.io.Serializable;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;

import com.szy.news.control.MyApplication;
import com.szy.news.model.Parameter;
import com.szy.news.service.SyncHttp;
import com.szy.news.util.URL;

/**
 * @author Administrator
 * 新闻主界面
 * */
public class NewsDetailsActivity extends Activity {
	private final int FINISH = 0;

	private ViewFlipper mNewsBodyFlipper;
	private LayoutInflater mNewsBodyInflater;
	private float mStartX;
	private ArrayList<HashMap<String, Object>> mNewsData;
	private int mPosition = 0;
	private int mCursor;
	private int mNid;
	private TextView mNewsDetails;
	private Button mNewsdetailsTitlebarComm;// 新闻回复数
	private ImageButton mNewsReplyImgBtn;// 发表新闻回复图片
	private LinearLayout mNewsReplyImgLayout;// 发表新闻回复图片Layout
	private LinearLayout mNewsReplyEditLayout;// 发表新闻回复回复Layout
	//1/6
//	private ScrollView news_body_scrollview;
	private TextView mNewsReplyContent;// 新闻回复内容
	private int count;			//新闻回复个数
	private SharedPreferences sp; 
	private MyApplication my;
	private ImageButton mShareNewsButton;		//分享新闻的按钮
	private ImageButton mFavoritesButton;		//赞按钮
	
	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
//			if(msg.what==1){
				switch (msg.arg1) {
				case FINISH:
					// 把获取到的新闻显示到界面上
					mNewsDetails.setText(Html.fromHtml(msg.obj.toString()));
					break;
				}
//			}
		}
	};

	@SuppressWarnings("unchecked")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.newsdetails);
		my=(MyApplication) this.getApplication();
		sp = getSharedPreferences("userdata", 0);
		
		
		
//		NewsDetailsTitleBarOnClickListener newsDetailsTitleBarOnClickListener = new NewsDetailsTitleBarOnClickListener();
		 // 查找新闻回复图片Layout
		mNewsReplyImgLayout = (LinearLayout) findViewById(R.id.news_reply_img_layout);
		// 查找新闻回复回复Layout
		mNewsReplyEditLayout = (LinearLayout) findViewById(R.id.news_reply_edit_layout);
		// 新闻回复内容
		mNewsReplyContent = (TextView) findViewById(R.id.news_reply_edittext);
		NewsDetailsOnClickListener newsDetailsOnClickListener = new NewsDetailsOnClickListener();
		// 上一篇新闻
		Button newsDetailsTitlebarPref = (Button) findViewById(R.id.newsdetails_titlebar_previous);
		newsDetailsTitlebarPref
				.setOnClickListener(newsDetailsOnClickListener);
		// 下一篇新闻
		Button newsDetailsTitlebarNext = (Button) findViewById(R.id.newsdetails_titlebar_next);
		newsDetailsTitlebarNext
				.setOnClickListener(newsDetailsOnClickListener);
		// 新闻回复条数Button
		mNewsdetailsTitlebarComm = (Button) findViewById(R.id.newsdetails_titlebar_comments);
		mNewsdetailsTitlebarComm.setOnClickListener(newsDetailsOnClickListener);
		// 发表新闻回复图片Button
		mNewsReplyImgBtn = (ImageButton) findViewById(R.id.news_reply_img_btn);
		mNewsReplyImgBtn.setOnClickListener(newsDetailsOnClickListener);
		// 发表回复
		Button newsReplyPost = (Button) findViewById(R.id.news_reply_post);
		newsReplyPost.setOnClickListener(newsDetailsOnClickListener);
		//	分享
		mShareNewsButton = (ImageButton) findViewById(R.id.news_share_btn);
		mShareNewsButton
				.setOnClickListener(newsDetailsOnClickListener);
		
		//	赞新闻
		mFavoritesButton = (ImageButton) findViewById(R.id.news_collect_btn);
		mFavoritesButton.setOnClickListener(newsDetailsOnClickListener);
		
		//1/6
		
		// 获取传递的数据
		Intent intent = getIntent();
		Bundle bundle = intent.getExtras();
		// 设置标题栏名称
		String categoryName = bundle.getString("categoryName");
		TextView titleBarTitle = (TextView) findViewById(R.id.newsdetails_titlebar_title);
		titleBarTitle.setText(categoryName);
		// 获取新闻集合
		Serializable s = bundle.getSerializable("newsDate");
		mNewsData = (ArrayList<HashMap<String, Object>>) s;
		// 获取点击位置
		mCursor = mPosition = bundle.getInt("position");

		// 动态创建新闻视图，并赋值
		mNewsBodyInflater = getLayoutInflater();
		inflateView(0);
			
	}
	
//	class News_body_details_layout implements OnTouchListener {
//		@Override
//		public boolean onTouch(View v, MotionEvent event) {
//			switch (event.getAction()) {
//			// 手指按下
//			case MotionEvent.ACTION_DOWN:
////				mNewsReplyImgLayout.setVisibility(View.VISIBLE);
////				mNewsReplyEditLayout.setVisibility(View.GONE);
////				InputMethodManager m = (InputMethodManager) mNewsReplyContent.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
////				m.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
//				// 记录起始坐标
//				mStartX = event.getRawX();
//				break;
//			// 手指抬起
//			case MotionEvent.ACTION_UP:
//				// 往左滑动
//				if (mStartX-event.getRawX()>3) {
//					showNext();
//				}
//				// 往右滑动
//				else if (event.getRawX()-mStartX>3) {
//					showPrevious();
//				}
//				break;
//			}
//			return true;
//		}
//	}
	
//	class test{
//		public boolean flag = false;
//
//		public boolean isFlag() {
//			return flag;
//		}
//
//		public void setFlag(boolean flag) {
//			this.flag = flag;
//		}
//	}
	/**
	 * 处理NewsDetailsTitleBar点击事件
	 */
	class NewsDetailsOnClickListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			InputMethodManager m = (InputMethodManager) mNewsReplyContent.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
			switch (v.getId()) {
			// 上一条新闻
			case R.id.newsdetails_titlebar_previous:
				showPrevious();
				break;
			// 下一条新闻
			case R.id.newsdetails_titlebar_next:
				showNext();
				break;
			// 显示评论
			case R.id.newsdetails_titlebar_comments:
				Intent intent = new Intent(NewsDetailsActivity.this,
						CommentsActivity.class);
				//传递新闻id
				intent.putExtra("nid", mNid);
				startActivity(intent);
				break;
			//新闻回复图片按钮
			case R.id.news_reply_img_btn:
				mNewsReplyImgLayout.setVisibility(View.GONE);
				mNewsReplyEditLayout.setVisibility(View.VISIBLE);
				mNewsReplyContent.requestFocus();
				m.toggleSoftInput(0, InputMethodManager.SHOW_IMPLICIT);
				break;
			//发表新闻回复
			case R.id.news_reply_post:
//				Toast.makeText(NewsDetailsActivity.this, ""+my.isFlag(), 1000).show();
				boolean flag=my.isFlag();
				if(flag==true){
					mNewsReplyEditLayout.post(new PostCommentThread());
					mNewsReplyImgLayout.setVisibility(View.VISIBLE);
					mNewsReplyEditLayout.setVisibility(View.GONE);
					m.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
//					intent = new Intent();
//					intent.setClass(NewsDetailsActivity.this, LoginActivity.class);
//					startActivity(intent);
//					finish();
				}else{
					intent = new Intent(NewsDetailsActivity.this, LoginActivity.class);
//					intent.setClass(NewsDetailsActivity.this, LoginActivity.class);
					startActivity(intent);
//					mNewsReplyEditLayout.post(new PostCommentThread());
//					mNewsReplyImgLayout.setVisibility(View.VISIBLE);
//					mNewsReplyEditLayout.setVisibility(View.GONE);
//					m.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
				}
				break;
			case R.id.news_share_btn:
				Intent shareIntent = new Intent(Intent.ACTION_SEND);
				//纯文本
				shareIntent.setType("text/plain");
				shareIntent.putExtra(Intent.EXTRA_SUBJECT, "分享");
				shareIntent.putExtra(Intent.EXTRA_TEXT, "我想将这个分享给你...."+ getNewsBody());
				startActivity(Intent.createChooser(shareIntent, getTitle()));
				break;
			case R.id.news_collect_btn:
				show(mNid);
				break;
			
			}

		}

	}
	
	
	
	/**
	 * 发表回复
	 */
	private class PostCommentThread extends Thread
	{
		@Override
		public void run()
		{
			SyncHttp syncHttp = new SyncHttp();
//			String url = "http://192.168.137.1:8080/web/postComment";
			List<Parameter> params = new ArrayList<Parameter>();
			params.add(new Parameter("nid", mNid + ""));
//			params.add(new Parameter("region", sp.getString("username", "匿名")));
			//LoginActivity s1 = new LoginActivity();
			// MyApplication ma=(MyApplication) getApplication();
			
//			params.add(new Parameter("region", my.getUsername()));
//			params.add(new Parameter("content", mNewsReplyContent.getText().toString()));
////			params.add(new Parameter("commentcount",count.get);
//			try
//			{
//				String retStr = syncHttp.httpPost(URL.postcomment_url, params);
//				JSONObject jsonObject = new JSONObject(retStr);
//				int retCode = jsonObject.getInt("ret");
//				if (0 == retCode)
//				{
//					Toast.makeText(NewsDetailsActivity.this, R.string.post_success, Toast.LENGTH_SHORT).show();
//					mNewsReplyImgLayout.setVisibility(View.VISIBLE);
//					mNewsReplyEditLayout.setVisibility(View.GONE);
//					return;
//				}
//
//			} catch (Exception e)
//			{
//				e.printStackTrace();
//			}
//			Toast.makeText(NewsDetailsActivity.this, R.string.post_failure, Toast.LENGTH_SHORT).show();
			String Content = mNewsReplyContent.getText().toString();
			if (!Content.equals("")) {
				params.add(new Parameter("region", my.getUsername()));
				params.add(new Parameter("content", Content));
				try
				{
					String retStr = syncHttp.httpPost(URL.postcomment_url, params);
					JSONObject jsonObject = new JSONObject(retStr);
					int retCode = jsonObject.getInt("ret");
					if (0 == retCode)
					{
						Toast.makeText(NewsDetailsActivity.this, R.string.post_success, Toast.LENGTH_SHORT).show();
						mNewsReplyImgLayout.setVisibility(View.VISIBLE);
						mNewsReplyEditLayout.setVisibility(View.GONE);
						return;
					}

				} catch (Exception e)
				{
					e.printStackTrace();
				}
				Toast.makeText(NewsDetailsActivity.this, R.string.post_failure, Toast.LENGTH_SHORT).show();
			}else {
				Toast.makeText(NewsDetailsActivity.this, "回复内容不能为空！", Toast.LENGTH_SHORT).show();
			}

			
		}
	}

	/**
	 * 处理新闻NewsBody触摸事件
	 */
	class NewsBodyOnTouchListener implements OnTouchListener {
		@Override
		public boolean onTouch(View v, MotionEvent event) {
			switch (event.getAction()) {
			// 手指按下
			case MotionEvent.ACTION_DOWN:
				mNewsReplyImgLayout.setVisibility(View.VISIBLE);
				mNewsReplyEditLayout.setVisibility(View.GONE);
//				InputMethodManager m = (InputMethodManager) mNewsReplyContent.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
//				m.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
				// 记录起始坐标
				mStartX = event.getRawX();
				break;
			// 手指抬起
			case MotionEvent.ACTION_UP:
				// 往左滑动
				if (mStartX-event.getRawX()>3) {
					showNext();
				}
				// 往右滑动
				else if (event.getRawX()-mStartX>3) {
					showPrevious();
				}
				break;
			}
			return true;
		}
	}

	/**
	 * 显示下一条新闻
	 */
	private void showNext() {
		// 判断是否是最后一篇win问
		if (mPosition < mNewsData.size() - 1) {
			// 设置下一屏动画
			mNewsBodyFlipper.setInAnimation(this, R.anim.push_left_in);
			mNewsBodyFlipper.setOutAnimation(this, R.anim.push_left_out);
			mPosition++;
			// 判断下一屏是否已经创建
			if (mPosition >= mNewsBodyFlipper.getChildCount()) {
				inflateView(mNewsBodyFlipper.getChildCount());
			}
			// 显示下一屏
			mNewsBodyFlipper.showNext();
		} else {
			Toast.makeText(this, R.string.no_next_news, Toast.LENGTH_SHORT)
					.show();
		}
		System.out.println(mCursor + ";" + mPosition);
	}
	
	//点赞
	public void show(Integer mNid) {
//		int[] i1 = new int[1];
//		String url="http://172.27.35.1:8080/web//DianzanServlet";
		MyApplication my=(MyApplication) this.getApplication();
		final String username=my.getUsername();
//		Toast.makeText(this, username, Toast.LENGTH_SHORT).show();
		FinalHttp fh = new FinalHttp();
		AjaxParams params = new AjaxParams();
		
		if(my.getUsername()==null){
			String version=android.os.Build.MODEL;
			params.put("method", "add_dianzan_jixing");
			params.put("n_id", mNid+"");
			params.put("version", version);
			fh.post(URL.dianzan_url, params, new AjaxCallBack<Object>() {
				@Override
				public void onSuccess(Object t) {
					super.onSuccess(t);
					String bool=(String) t;
//					Toast.makeText(NewsDetailsActivity.this, bool, Toast.LENGTH_SHORT).show();
					if("true".equals(bool.trim())){
						Toast.makeText(NewsDetailsActivity.this, "该匿名用户已赞", Toast.LENGTH_SHORT).show();
					}else if("false".equals(bool.trim())){
						Toast.makeText(NewsDetailsActivity.this, "点赞+1", Toast.LENGTH_SHORT).show();
					}
				}
			});
		}else{
			params.put("method", "add_dianzan");
			params.put("n_id", mNid+"");
			params.put("username", username);
			fh.post(URL.dianzan_url, params, new AjaxCallBack<Object>() {
				@Override
				public void onSuccess(Object t) {
					super.onSuccess(t);
					String bool=(String) t;
//					Toast.makeText(NewsDetailsActivity.this, bool, Toast.LENGTH_SHORT).show();
					if("true".equals(bool.trim())){
						Toast.makeText(NewsDetailsActivity.this, "用户"+username+"已赞", Toast.LENGTH_SHORT).show();
					}else if("false".equals(bool.trim())){
						Toast.makeText(NewsDetailsActivity.this, "点赞+1", Toast.LENGTH_SHORT).show();
					}
				}
			});
		}
//		FinalHttp fh = new FinalHttp();
//		AjaxParams params = new AjaxParams();
//		params.put("n_id", mNid+"");
//		fh.post(URL.dianzan_url, params, new AjaxCallBack<Object>() {
//			@Override
//			public void onSuccess(Object t) {
//				super.onSuccess(t);
//				String id=(String) t;
////				Toast.makeText(NewsDetailsActivity.this, "是否有新闻："+id, Toast.LENGTH_SHORT).show();
//				if("1".equals(id.trim())){
//					Toast.makeText(NewsDetailsActivity.this, "您已经点过赞", Toast.LENGTH_SHORT).show();
//				}else{
//					Toast.makeText(NewsDetailsActivity.this, "赞+1！", Toast.LENGTH_SHORT).show();
//				}
//			}
//		});

		
//		i1[0] = mNid;
//		Toast.makeText(this, ""+mNid, Toast.LENGTH_SHORT).show();
//		mFavoritesButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.dark_rating_favorite2));
//		HashMap<String	, Integer> C_hasp=new HashMap<String, Integer>();
//		String num=String.valueOf(mNid);
//		if(my.getC_hasp()==null){
//		C_hasp.put(num, mNid);
//		my.setC_hasp(C_hasp);
//		}
//		else if(mNid==my.getC_hasp().get(num)){
//		Toast.makeText(this, ""+my.getC_hasp().get(num), Toast.LENGTH_SHORT).show();
//		}
//		my = (MyApplication) this.getApplication();
////		List<Integer> dian = my.getDianzan();
//		if(my.getDianzan()!=null){
//			if(my.getDianzan().get(0)!=null){
//			Toast.makeText(this, "非空"+my.dianzan.get(0), Toast.LENGTH_SHORT).show();
//			}
//			for(int i = 0;i<my.getDianzan().size();i++){
//				if(my.getDianzan().get(i)==mNid&&my.getDianzan().get(i)!=null){
//					Toast.makeText(this, "for"+my.dianzan.get(0), Toast.LENGTH_SHORT).show();
//				}
//			}
//		}
//		else{
//			my.getDianzan().add(1);
//			Toast.makeText(this, ""+mNid, Toast.LENGTH_SHORT).show();
//		}
		
		
	}

	private void showPrevious() {
		if (mPosition > 0) {
			mPosition--;
			// 记录当前新闻编号
			HashMap<String, Object> hashMap = mNewsData.get(mPosition);
			mNid = (Integer) hashMap.get("nid");
			
			
			if (mCursor > mPosition) {
				mCursor = mPosition;
				inflateView(0);
				System.out.println(mNewsBodyFlipper.getChildCount());
				mNewsBodyFlipper.showNext();// 显示下一页
			}
			mNewsBodyFlipper.setInAnimation(this, R.anim.push_right_in);// 定义下一页进来时的动画
			mNewsBodyFlipper.setOutAnimation(this, R.anim.push_right_out);// 定义当前页出去的动画
			mNewsBodyFlipper.showPrevious();// 显示上一页
		} else {
			Toast.makeText(this, R.string.no_pre_news, Toast.LENGTH_SHORT)
					.show();
		}
		System.out.println(mCursor + ";" + mPosition);
		
		
	}

	private void inflateView(int index) {
		// 动态创建新闻视图，并赋值
		View newsBodyLayout = mNewsBodyInflater.inflate(R.layout.news_body,
				null);
		// 获取点击新闻基本信息
		HashMap<String, Object> hashMap = mNewsData.get(mPosition);
		// 新闻标题
		TextView newsTitle = (TextView) newsBodyLayout
				.findViewById(R.id.news_body_title);
		newsTitle.setText(hashMap.get("newslist_item_title").toString());
		// 发布时间和出处
		TextView newsPtimeAndSource = (TextView) newsBodyLayout
				.findViewById(R.id.news_body_ptime_source);
		newsPtimeAndSource.setText(hashMap.get("newslist_item_ptime")
				.toString()
				+ "    "
				+ hashMap.get("newslist_item_source").toString());
		// 新闻编号
		mNid = (Integer) hashMap.get("nid");
		// 新闻回复数
//		mNewsdetailsTitlebarComm.setText(hashMap.get("newslist_item_comments")
//				+ "跟帖");

		// 把新闻视图添加到Flipper中
		mNewsBodyFlipper = (ViewFlipper) findViewById(R.id.news_body_flipper);
		mNewsBodyFlipper.addView(newsBodyLayout, index);

		// 给新闻Body添加触摸事件
		mNewsDetails = (TextView) newsBodyLayout
				.findViewById(R.id.news_body_details);
		
		
		mNewsDetails.setOnTouchListener(new NewsBodyOnTouchListener());
		//1/6
//		news_body_scrollview=(ScrollView) newsBodyLayout.findViewById(R.id.news_body_scrollview);
//		news_body_scrollview.setOnTouchListener(new NewsBodyOnTouchListener());

		// 启动线程
		new UpdateNewsThread().start();
	}

	private class UpdateNewsThread extends Thread {
		@Override
		public void run() {
			// 从网络上获取新闻
			String newsBody = getNewsBody();
			Message msg = mHandler.obtainMessage();
			msg.arg1 = FINISH;
			msg.obj = newsBody;
			mHandler.sendMessage(msg);
		}
	}

	/**
	 * 获取新闻详细信息
	 * 
	 * @return
	 */
	private String getNewsBody() {
		String retStr = "网络连接失败，请稍后再试";
		SyncHttp syncHttp = new SyncHttp();
//		String url = "http://192.168.137.1:8080/web/getNews";
		String params = "nid=" + mNid;
		try {
			String retString = syncHttp.httpGet(URL.NewsDetails_url, params);
			JSONObject jsonObject = new JSONObject(retString);
			// 获取返回码，0表示成功
			int retCode = jsonObject.getInt("ret");
			if (0 == retCode) {
				JSONObject dataObject = jsonObject.getJSONObject("data");
				JSONObject newsObject = dataObject.getJSONObject("news");
				retStr = newsObject.getString("body");
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return retStr;
		
		
	}
}
